from web3 import Web3

url = 'HTTP://127.0.0.1:8545'
privateKey = '0x455c59b94d743b4fd8b7f7d7234d5f76cccc8d5806e5f8f463d65703f0d4376b'
toAddress = '0x87C0d15163296d11349B8e3E9FAa4f018bA82315'

# 连接到以太坊节点
w3 = Web3(Web3.HTTPProvider(url))
chain_id = w3.eth.chain_id

# 确保连接成功
assert w3.is_connected()

# 设置你的账户
account = w3.eth.account.from_key(privateKey)

# 构建交易
nonce = w3.eth.get_transaction_count(account.address)
transaction = {
    'to': toAddress,
    'value': w3.to_wei(1, 'ether'),
    'gas': 200000,
    'gasPrice': w3.to_wei('1', 'gwei'),
    'nonce': nonce,
    'chainId': chain_id
}

# 签署交易
signed_txn = w3.eth.account.sign_transaction(transaction, privateKey)

# 发送交易
txn_hash = w3.eth.send_raw_transaction(signed_txn.rawTransaction)


# 获取交易哈希
print(f"交易哈希: {txn_hash.hex()}")
